package com.cw.jeeyt.dao.domain.security;

import java.util.ArrayList;
import java.util.List;

import com.cw.jeeyt.dao.model.security.SecurityResourceDo;

import lombok.Data;

/**
 * 资源管理菜单树
 *
 * @author mengxw
 */
@Data
public class ResourceTreeNode {

    private String nodeId;
    private String parentNodeId;
    private SecurityResourceDo nodeData;
    private List<ResourceTreeNode> children = new ArrayList<ResourceTreeNode>();

    /**
     * 根据NodeId获取相应的节点对象
     *
     * @param nodeId
     * @return
     */
    public List<ResourceTreeNode> getByNodeId(String nodeId) {
        if (nodeId.equals(this.nodeId)) {
            List<ResourceTreeNode> resulTreeNodes = new ArrayList<ResourceTreeNode>();
            resulTreeNodes.add(this);
            return resulTreeNodes;
        } else {
            return recurSearch(this, nodeId);
        }
    }

    /**
     * 递归查询需要的节点
     *
     * @param treeNode
     * @param nodeId
     * @return
     */
    public List<ResourceTreeNode> recurSearch(ResourceTreeNode treeNode, String nodeId) {
        List<ResourceTreeNode> children = treeNode.getChildren();
        boolean flag = false;
        List<ResourceTreeNode> nodeList = new ArrayList<ResourceTreeNode>();
        int i = 0;
        for (ResourceTreeNode node : children) {
            i++;
            if (node.getNodeId()
                .equals(nodeId)) {
                flag = true;
                nodeList.add(node);
            }
            if (flag && i == children.size()) {
                return nodeList;
            }
        }
        if (!flag) {
            for (ResourceTreeNode node : children) {
                List<ResourceTreeNode> result = recurSearch(node, nodeId);
                if (result == null) {
                    continue;
                } else {
                    return result;
                }
            }
        }
        return null;
    }

}
